import java.util.HashSet;
import java.util.Set;

/**
 * @ClassName LC202
 * @Author ykx
 * @Date 2024/3/28 21:46
 * @Version 1.0
 */
public class LC202 {
    public static void main(String[] args) {
        LC202 lc202 = new LC202();
        System.out.println(lc202.isHappy(19));
    }

    public boolean isHappy(int n) {
        Set<Integer> integers = new HashSet<>();
        return solve(n, integers);
    }

    public boolean solve(int num, Set<Integer> integers) {
        boolean flag = true;
        if (num != 1) {
            String s = num + "";
            int n = 0;
            for (int i = 0; i < s.length(); i++) {
                n += (s.charAt(i) - '0') * (s.charAt(i) - '0');
            }
            if (!integers.add(n)) {
                return false;
            }
            flag = solve(n, integers);
        }
        return flag;
    }

}
